﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace AJAXChatClient
{
    class XmlHelpers
    {
        public static string GetTitle(XmlDocument doc)
        {
            XmlNodeList titles = doc.GetElementsByTagName("title");
            if (titles.Count == 0)
                return "";
            return titles[0].Value;
        }

        public static void SetTitle(XmlDocument doc, string title)
        {
            XmlNode titleNode;
            XmlNodeList titles = doc.GetElementsByTagName("title");
            if (titles.Count == 0)
            {
                titleNode = doc.CreateElement("title");
                titleNode.Value = title;
                XmlNode headNode;
                XmlNodeList heads = doc.GetElementsByTagName("head");
                if (heads.Count == 0)
                {
                    headNode = doc.CreateElement("head");
                    XmlNode html = doc.GetElementsByTagName("html")[0];
                    html.AppendChild(headNode);
                }
                else
                {
                    headNode = heads[0];
                }
                headNode.AppendChild(titleNode);
            }
            else
            {
                titleNode = titles[0];
            }
            titleNode.Value = title;
        }

        public static XmlNode GetFirstChildWithTagName(XmlNode node, string tagName)
        {
            XmlNodeList children = node.ChildNodes;
            if (children == null || children.Count == 0)
                return null;
            XmlNode child;
            for (int i = 0; i < children.Count; i++)
            {
                child = children[i];
                if (tagName.Equals(child.Name, StringComparison.Ordinal))
                    return child;
            }
            return null;
        }

        public static void SetAttribute(XmlNode node, string name, string value)
        {
            XmlAttribute attr = node.Attributes[name];
            if (attr == null)
            {
                attr = node.OwnerDocument.CreateAttribute(name);
                attr.Value = value;
                node.Attributes.Append(attr);
            }
            else
            {
                attr.Value = value;
            }
        }

        public static bool GetDisabled(XmlNode node)
        {
            XmlAttribute disabledAttr = node.Attributes["disabled"];
            if (disabledAttr == null || string.IsNullOrEmpty(disabledAttr.Value))
            {
                return false;
            }
            bool disabled;
            if (!bool.TryParse(disabledAttr.Value, out disabled))
            {
                return false;
            }
            return disabled;
        }

        public static void SetDisabled(XmlNode node, bool disabled)
        {
            XmlAttribute disabledAttr = node.Attributes["disabled"];
            if (disabledAttr == null)
            {
                disabledAttr = node.OwnerDocument.CreateAttribute("disabled");
                disabledAttr.Value = disabled ? "true" : "false";
                node.Attributes.Append(disabledAttr);
            }
            else
            {
                disabledAttr.Value = disabled ? "true" : "false";
            }
        }
    }
}
